package fr.labri.harmony.analysis.jsep.results.filter;

import java.io.IOException;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import fr.labri.harmony.analysis.jsep.results.LoadRules;

public class MigrationRulesSummary extends ResultFilter {

	@Override
	public void computeResult(ResultSet rs) throws SQLException, IOException {
		rs.first();
		PrintStream ps = new PrintStream(System.out);
		PrintStream ps_true = new PrintStream("Rules_true.csv");
		Map<String,Integer> rules = new HashMap<String, Integer>();
		Set<String> libs = new HashSet<String>();
		int intro_lib = 0;
		int nb_migrations = 0;
		ps_true.println("Source,Target,Score");
		while (rs.next()){		
			intro_lib++;
			String source = rs.getString("NAME");
			String target = rs.getString("MIGRATION_LIB_TO");
			if(target != null) {
				nb_migrations++;
				if(LoadRules.isTrueRule(source, target)) {
					if(!rules.containsKey(source+","+target))
						rules.put(source+","+target, 0);
					rules.put(source+","+target, rules.get(source+","+target)+1);
					libs.add(source);
					libs.add(target);
				}
			}
		}
		for(String rule : rules.keySet()) {
			ps_true.println(rule+","+rules.get(rule));
		}	
		ps_true.close();
		int nbRules=rules.size();
		int nbMigration=0;
		for(int value : rules.values())
			nbMigration+=value;
		ps.println("Nombre d'introductions de librairies : "+intro_lib);
		ps.println("Nombre de migrations : "+nb_migrations);
		ps.println("Nombre de vraies règles de migrations : "+nbRules);
		ps.println("Nombre de vraies migrations : "+nbMigration);
		ps.println("Nombre de catégories : "+LoadRules.getCategories().size());
		ps.println("Nombre de librairies impliquées dans les migrations : "+libs.size());
	}

}

